<template>
  <div id="app">

    <!-- 搜索框 -->
    <el-card class="box-card" shadow="always">
      <el-input
        placeholder="商品名"
        v-model="selectStock.goodsName"
        size="medium">
      </el-input>
      <el-input
        placeholder="起始库存"
        v-model="selectStock.startGoodCount"
        size="medium">
      </el-input>
      <el-input
        placeholder="截至库存"
        v-model="selectStock.endGoodCount"
        size="medium">
      </el-input>
      <el-button type="info" size="medium" @click="getStockList" plain>查询</el-button>
      <el-button type="warning" size="medium" @click="handelRecet" plain>重置</el-button>
    </el-card>

    <!-- 表格以及分页 -->
    <el-card shadow="always">
       <el-table
        :data="tableData"
        style="width: 100%"
        height="490">
        <el-table-column
          label="商品名"
          width="100"
          prop="goodsName">
        </el-table-column>
        <el-table-column
            label="图片"
            width="180"
            align="center">
            <template slot-scope="scope">
              <el-image
                :src="scope.row.goodsImg"
                fit="fit">
              </el-image>
            </template>
        </el-table-column>
        <el-table-column
          label="库存数量"
          width="180"
          align="center"
          prop="goodsCount">
          <template #default="{ row }">
            <span :style="{ color: row.goodsCount <= 5 ? '#F56C6C' : '#409EFF' }"> {{ row.goodsCount }} </span>
          </template>
        </el-table-column>
        <el-table-column
          label="最后更新日期"
          prop="createDate">
        </el-table-column>
      </el-table>

      <div class="block">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[5, 10, 20, 50]"
          :page-size="pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </div>

    </el-card>
  </div>

</template>

<script>
import instance from '@/utils/request'
export default {
  name: 'EmployeePage',
  data () {
    return {
      selectStock: {
        goodsName: '',
        startGoodCount: '',
        endGoodCount: ''
      },
      currentPage: 1, // 当前页
      pageSize: 5,
      total: 1,
      tableData: []
    }
  },

  methods: {
    handleSizeChange (val) {
      console.log(`每页 ${val} 条`)
      this.pageSize = val
      this.getStockList()
    },
    handleCurrentChange (val) {
      console.log(`当前页: ${val}`)
      this.currentPage = val
      this.getStockList()
    },
    handelRecet () {
      this.selectStock = {
        goodsName: '',
        startGoodCount: '',
        endGoodCount: ''
      }
      this.getStockList()
    },
    // 表格数据渲染
    // 渲染表格
    async getStockList () {
      const res = await instance.post(`/stock/list/${this.currentPage}/${this.pageSize}`,
        this.selectStock)
      this.tableData = res.data.list
      this.total = res.data.total
    }
  },

  created () {
    this.getStockList()
  }

}
</script>

<style scoped>
.box-card{
  margin-bottom: 5px;
}
.el-input{
  width: 200px;
  margin-right: 5px;
}
.el-card ::v-deep .el-card__body {
  padding: 10px;
}
.el-pagination {
  padding: 15px;
}
</style>
